﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameEngine.Managers.IManagers;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;

namespace GameEngine.Managers.ConcreteManagers
{
    public class AudioManager: IAudioManager
    {
        #region Fields

        private Game game;
        private Dictionary<String, SoundEffect> soundEffects;
        private SoundEffectInstance sei;
        private float globalVolume = 1.0f;

        #endregion
        #region Properties

        /// <summary>
        /// Gets or sets the global volume.
        /// </summary>
        /// <value>
        /// The global volume.
        /// </value>
        public float GlobalVolume
        {
            get { return globalVolume; }
            set { globalVolume = value; }
        }

        #endregion

        public AudioManager(Game game)
        {
            this.game = game;
            this.soundEffects = new Dictionary<string, SoundEffect>();
        }

        /// <summary>
        /// Plays the sound.
        /// </summary>
        /// <param name="sound">The sound.</param>
        /// <param name="volume">The volume.</param>
        public void PlaySound(string sound, float volume)
        {
            SoundEffect soundE;
            soundEffects.TryGetValue(sound, out soundE);

            if (soundE == null)
                return;

            soundE.Play(volume * globalVolume, 0.0f, 0.0f);
        }

        /// <summary>
        /// Stops the looped sound.
        /// </summary>
        /// <param name="sound">The sound.</param>
        public void StopLoopedSound(string sound)
        {
            sei.Stop();
        }


        /// <summary>
        /// Plays the looped sound.
        /// </summary>
        /// <param name="sound">The sound.</param>
        /// <param name="volume">The volume.</param>
        public void PlayLoopedSound(string sound, float volume)
        {
            SoundEffect soundE;
            soundEffects.TryGetValue(sound, out soundE);

            if (soundE == null)
                return;

            sei = soundE.CreateInstance();
            sei.IsLooped = true;
            sei.Pitch = 1.0f;
            sei.Volume = globalVolume * volume;
            sei.Play();
        }

        /// <summary>
        /// Adds the sound effect.
        /// </summary>
        /// <param name="sound">The sound.</param>
        /// <param name="name">The name.</param>
        public void AddSoundEffect(Microsoft.Xna.Framework.Audio.SoundEffect sound, string name)
        {
            try
            {
                soundEffects.Add(name, sound);
            }
            catch (System.ArgumentException)
            {
            }
        }

        /// <summary>
        /// Gets the sound effect.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public SoundEffect GetSoundEffect(string name)
        {
            SoundEffect soundE;
            soundEffects.TryGetValue(name, out soundE);

            return soundE;
        }
    }
}
